// pages/douban/list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 'in_theaters',
    page: 1,
    size: 10,
    total: 20,
    loading: true,
    isMoveToUp: true,
    movies: [
      {
        id: 1,
        title: "喵星人",
        year: "2018",
        images: {
          small: "https://images.unsplash.com/photo-1636421629832-4e5ba1baed40?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=690&q=80"
        },
        rating: {
          average: 7.8
        },
        directors: [
          {
            id: 1,
            name: "李安"
          },
          {
            id: 2,
            name: "陈凯歌"
          }
        ],
        casts: [
          {
            id: 1,
            name: "沈腾"
          },
          {
            id: 2,
            name: "陈赫"
          }
        ],
        summary: "一次彗星撞击，世界陷入一顿恐慌之中……"
      },
      {
        id: 2,
        title: "喵星人",
        year: "2018",
        images: {
          small: "https://images.unsplash.com/photo-1636421629832-4e5ba1baed40?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=690&q=80"
        },
        rating: {
          average: 7.8
        },
        directors: [
          {
            id: 1,
            name: "李安"
          },
          {
            id: 2,
            name: "陈凯歌"
          }
        ],
        casts: [
          {
            id: 1,
            name: "沈腾"
          },
          {
            id: 2,
            name: "陈赫"
          }
        ],
        summary: "一次彗星撞击，世界陷入一顿恐慌之中……"
      },
      {
        id: 3,
        title: "喵星人",
        year: "2018",
        images: {
          small: "https://images.unsplash.com/photo-1636421629832-4e5ba1baed40?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=690&q=80"
        },
        rating: {
          average: 7.8
        },
        directors: [
          {
            id: 1,
            name: "李安"
          },
          {
            id: 2,
            name: "陈凯歌"
          }
        ],
        casts: [
          {
            id: 1,
            name: "沈腾"
          },
          {
            id: 2,
            name: "陈赫"
          }
        ],
        summary: "一次彗星撞击，世界陷入一顿恐慌之中……"
      },
      {
        id: 4,
        title: "喵星人",
        year: "2018",
        images: {
          small: "https://images.unsplash.com/photo-1636421629832-4e5ba1baed40?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=690&q=80"
        },
        rating: {
          average: 7.8
        },
        directors: [
          {
            id: 1,
            name: "李安"
          },
          {
            id: 2,
            name: "陈凯歌"
          }
        ],
        casts: [
          {
            id: 1,
            name: "沈腾"
          },
          {
            id: 2,
            name: "陈赫"
          }
        ],
        summary: "一次彗星撞击，世界陷入一顿恐慌之中……"
      },
      {
        id: 5,
        title: "喵星人",
        year: "2018",
        images: {
          small: "https://images.unsplash.com/photo-1636421629832-4e5ba1baed40?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=690&q=80"
        },
        rating: {
          average: 7.8
        },
        directors: [
          {
            id: 1,
            name: "李安"
          },
          {
            id: 2,
            name: "陈凯歌"
          }
        ],
        casts: [
          {
            id: 1,
            name: "沈腾"
          },
          {
            id: 2,
            name: "陈赫"
          }
        ],
        summary: "一次彗星撞击，世界陷入一顿恐慌之中……"
      },
      {
        id: 6,
        title: "喵星人",
        year: "2018",
        images: {
          small: "https://images.unsplash.com/photo-1636421629832-4e5ba1baed40?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=690&q=80"
        },
        rating: {
          average: 7.8
        },
        directors: [
          {
            id: 1,
            name: "李安"
          },
          {
            id: 2,
            name: "陈凯歌"
          }
        ],
        casts: [
          {
            id: 1,
            name: "沈腾"
          },
          {
            id: 2,
            name: "陈赫"
          }
        ],
        summary: "一次彗星撞击，世界陷入一顿恐慌之中……"
      }
    ]
  },

  loadMorePage() {
    if (this.data.page > this.data.total) { return false }
    this.data.page++;
    this.retrieve()
  },

  retrieve() {
    wx.showLoading({
      title: '加载中',
    });

    console.log("页码增加了", this.data.page)

    let movies = this.data.movies.concat(this.data.movies)
    let total = Math.floor(40 / this.data.size);
    this.setData({ movies: movies, total: total, page: this.data.page });
    // return wx.request({
    //     url: `https://api.douban.com/v2/movie/${this.data.type}?page=${this.data.page}&count=${this.page.size}`,
    //     data: {
    //         page: this.data.page,
    //         pageSize: this.data.size,
    //     },
    //     success(res) {
    //         this.setData({movie: d, loading: false});
    //         wx.setNavigationBarTitle({
    //             title: d.title,
    //         }).catch(e => {
    //             console.error(e);
    //         })
    //     },
    //     fail(err) {
    //         console.error(err)
    //     },
    //     complete() {
    //         wx.hideLoading()
    //     }
    // })

    setTimeout(() => {
      this.setData({ loading: false })
      wx.hideLoading()
    }, 1000);
  },

  onRefresh() {
    this.setData({                       //MoveToUp触发
      isMoveToUp: false
    })
    setTimeout(() => {
      this.retrieve()
      wx.stopPullDownRefresh()
    }, 1000)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.retrieve()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   * body整体滚动事件
   */
  onPullDownRefresh: function () {
    // 貌似不起作用
    console.log(333)
    this.retrieve().then(() => wx.stopPullDownRefresh())
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})